package com.wyh.rabbitextensionsdemo.controller;

import com.wyh.rabbitextensionsdemo.constant.Constants;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/produce")
public class ProducerController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @Autowired
    RabbitTemplate confirmRabbitTemplate;

    @Autowired
    RabbitTemplate tranRabbitTemplate;

    @RequestMapping("/ack")
    public String ack(){
        String msg = "hello ack~";
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,Constants.ACK_ROUTING_KEY,msg);
        return "消息发送成功";
    }

    @RequestMapping("/pre1")
    public String pre1(){
        String msg = "hello pre1~";
        Message message = new Message(msg.getBytes(), new MessageProperties());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.NON_PERSISTENT);
        rabbitTemplate.convertAndSend(Constants.ACK_EXCHANGE,Constants.ACK_ROUTING_KEY,message);
        return "消息发送成功";
    }

    @RequestMapping("/pre2")
    public String pre2(){
        String msg = "hello pre2~";
        Message message = new Message(msg.getBytes(), new MessageProperties());
        message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(Constants.PRE_EXCHANGE,Constants.PRE_ROUTING_KEY,message,correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/confirm")
    public String confirm(){
        String msg = "hello confirm~";
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(Constants.CONFIRM_EXCHANGE + "1", Constants.CONFIRM_ROUTING_KEY,msg,correlationData);
        return "消息发送成功";
    }

    @RequestMapping("/ttl")
    public String ttl(){
        String s = "hello ttl~";
        Message msg = new Message(s.getBytes(),new MessageProperties());
        msg.getMessageProperties().setExpiration("10000");
        rabbitTemplate.convertAndSend(Constants.TTL_EXCHANGE,Constants.TTL_ROUTING_KEY,msg);
        return "消息发送成功";
    }

    @RequestMapping("/normal")
    public String normal (){
//        String s = "hello dl~";
//        Message msg = new Message(s.getBytes(),new MessageProperties());
////        msg.getMessageProperties().setExpiration("10000");
//        rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,Constants.NORMAL_ROUTING_KEY,msg);

        for (int i = 0; i < 4; i++){
            String s = "hello dl~" + i;
            rabbitTemplate.convertAndSend(Constants.NORMAL_EXCHANGE,Constants.NORMAL_ROUTING_KEY,s);
        }
        return "消息发送成功";
    }

    @RequestMapping("/delay")
    public String delay(){
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,Constants.DELAY_ROUTING_KEY
                ,"delay test 10s..." + new Date(), message -> {
                    message.getMessageProperties().setDelayLong(10000L);
                    return message;
                });
        rabbitTemplate.convertAndSend(Constants.DELAY_EXCHANGE,Constants.DELAY_ROUTING_KEY
                ,"delay test 30s..." + new Date(), message -> {
                    message.getMessageProperties().setDelayLong(30000L);
                    return message;
                });
        System.out.println("消息发送成功：" + new Date());
        return "消息发送成功";
    }

    @Transactional
    @RequestMapping("/tran")
    public String tran(){
        tranRabbitTemplate.convertAndSend(Constants.TRAN_EXCHANGE,Constants.TRAN_ROUTING_KEY,"tran1");
        int n = 3/0;
        tranRabbitTemplate.convertAndSend(Constants.TRAN_EXCHANGE,Constants.TRAN_ROUTING_KEY,"tran2");
        return "消息发送成功";
    }

    @RequestMapping("/qos")
    public String qos(){
        for (int i = 0; i < 10; i++){
            String msg = "qos ~ " + i;
            rabbitTemplate.convertAndSend(Constants.QOS_EXCHANGE,Constants.QOS_ROUTING_KEY,msg);
        }
        return "消息发送成功";
    }
}
